package com.p7.framework.common.exception.handle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.p7.framework.common.ErrorBean;
import com.p7.framework.common.ResultBean;
import com.p7.framework.common.exception.RequestParamException;
import com.p7.framework.common.validator.LocaleService;

/**
 * 
 * @ClassName: ExceptionHandle
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author p7+
 * @date 2018年10月13日 下午8:43:50
 * @Copyright: 2018
 *
 */
@RestControllerAdvice
public class ExceptionHandle extends ResponseEntityExceptionHandler {

	@Autowired
	private LocaleService localeService;

	@ExceptionHandler(RequestParamException.class)
	public ErrorBean handleRequestParamException(RequestParamException e) {

		return new ErrorBean(localeService.getMessage(e.getErrMsg()));
	}

	protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
			HttpHeaders headers, HttpStatus status, WebRequest request) {
		ex.printStackTrace();
		return new ResponseEntity<Object>(
				new ResultBean(400, localeService.getMessage("system.http_request_method_not_supported")), headers,
				HttpStatus.OK);
	}

	protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex,
			HttpHeaders headers, HttpStatus status, WebRequest request) {
		ex.printStackTrace();
		return new ResponseEntity<Object>(
				new ResultBean(400, localeService.getMessage("system.missing_servlet_request_parameter")), headers,
				HttpStatus.OK);
	}

}
